package b3.widget.basic
{
	import b3.util.*;
	import b3.net.*;
	import b3.widget.basic.*;
	
	import flash.events.MouseEvent;
	import flash.text.*;
	import flash.filters.*;

	public class MessageBox extends BasicWidget
	{
		private var onOKCallFunction:Function;
		private var onOKParams:Array;

		public function MessageBox(message:String, hasCacelButton:Boolean = false)
		{
			super();
			this.anchor = ANCHOR_MIDDLE_MIDDLE;
			var DialogBgr:Class = ResourceHelper.getInstance().getClassByName("MessageBoxBgr");
			addChild(new DialogBgr());
			
			var textField:TextField = new TextField();
			textField.selectable = false;
        	textField.autoSize = TextFieldAutoSize.CENTER;
        	textField.multiline = true;
			textField.defaultTextFormat = new TextFormat(BFont.fontName, 20, 0xFFFFFF, true);
			textField.filters = [new GlowFilter(0x00AAFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			textField.x = this.width / 2;
			textField.y = 80;
			textField.htmlText = message;
			addChild(textField);
			
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr0");
			var buttonOK:BasicButton = new BasicButton(ButtonBgr, "确定");
			buttonOK.addEventListener(MouseEvent.CLICK, onButtonOKClick);
			if (!hasCacelButton)
			{
				buttonOK.x = (this.width  - buttonOK.width) / 2;
			}
			else
			{
				buttonOK.x = this.width / 2 - buttonOK.width - 10;
			}
			buttonOK.y = 220;
			buttonOK.addEventListener(MouseEvent.CLICK, onButtonOKClick);
			addChild(buttonOK);
			var buttonCacel:BasicButton;
			if (hasCacelButton)
			{
				buttonCacel = new BasicButton(ButtonBgr, "取消");
				buttonCacel.x = this.width / 2 + 10;
				buttonCacel.y = buttonOK.y;
				addChild(buttonCacel);
				buttonCacel.addEventListener(MouseEvent.CLICK, onButtonCancelClick);
			}
			this.x = -this.width / 2;
			this.y = -this.height / 2;
		}
		
		public function onButtonOKClick(event:MouseEvent):void
		{
			close();
			if (onOKCallFunction != null)
			{
				onOKCallFunction.apply(null, onOKParams);
				onOKCallFunction = null;
			}
		}
		
		public function setOKCall(onOKCallFunction:Function, onOkParams:Array = null):void
		{
			this.onOKCallFunction = onOKCallFunction;
			this.onOKParams = onOkParams;
		}
		
		private function onButtonCancelClick(event:MouseEvent):void
		{
			close();
		}
	}
}